/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.controlador;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.Mensaje;
import sv.com.intesal.conexion.cConexion;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.sql.SQLArea;

/**
 *
 * @author JM
 */
public class AreaControlador {

    public void mantAreaIM(Area area) throws Exception {
        Connection cnn = cConexion.conectar();
        try {
            PreparedStatement pstm;
            if (area.getCodigo() == 0) {
                // SE INGRESARA
                pstm = cnn.prepareStatement(SQLArea.areaInsertar);
            } else {
                // SE MODIFICARA
                pstm = cnn.prepareStatement(SQLArea.areaUpdate);
                pstm.setInt(2, area.getCodigo());
            }
            pstm.setString(1, area.getNombre());
            pstm.executeUpdate();
            pstm.close();
            cnn.close();

            Mensaje m = new Mensaje();
            if (area.getCodigo() == 0) {
                m.messageInfo(m.getIinformation());
            } else {
                m.messageInfo(m.getImodificada());
            }
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public List<Area> fillGrid() throws Exception {
        List<Area> list = new ArrayList<Area>();
        try {
            ResultSet rs = cConexion.conectar().prepareStatement(SQLArea.areaBuscar).executeQuery();
            while (rs.next()) {
                Area a = new Area();
                a.setCodigo(rs.getInt("id_a"));
                a.setNombre(rs.getString("nombre_a"));
                list.add(a);
            }
            rs.close();
            return list;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

     public List<Area> fillComboBox() throws Exception {
        try {
            return this.fillGrid();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
